;;;; RLlib copyright 2010 Adam White (theroguemonkey@gmail.com)
;;;; Licensed under GNU General Public Licence v2 or later - refer COPYING

(in-package #:rllib)

(let* ((dir (merge-pathnames (pathname #+darwin "dlls/osx/"
				       #+windows "dlls/win32/" 
				       #+linux "dlls/linux/")
			     (asdf:component-pathname (asdf:find-system :rllib)))))
  (pushnew dir cffi:*foreign-library-directories*)
      
  #+darwin
  (progn
    (cffi:define-foreign-library cocoahelper (:darwin "cocoahelper.dylib"))
    (cffi:use-foreign-library cocoahelper)
    (cffi:defcfun "cocoahelper_init" :void)
    (cocoahelper-init))

  (cffi:define-foreign-library sdl
    (:darwin (:or (:framework "SDL") "SDL.dylib"))
    (:windows "SDL.dll")
    (:unix (:or "libSDL-1.2.so"
                "libSDL.so")))

  (cffi:define-foreign-library freetype
    (:darwin (:or (:framework "FreeType") "FreeType.dylib"))
    (:windows "freetype6.dll")
    (:unix "libfreetype.so"))
  
  (cffi:define-foreign-library opengl
    (:darwin (:framework "OpenGL"))
    (:windows "opengl32.dll" :convention :stdcall)
    (:unix (:or "libGL.so"
		"libGL.so.2"
		"libGL.so.1")))

   (cffi:define-foreign-library glft
    (:darwin "glft.dylib")
    (:windows "glft.dll")
    (:unix "glft.so"))

  (cffi:define-foreign-library devil-il
    (:darwin (:framework "DevIL"))
    (:unix (:or "libIL" "libIL.so.1"))
    (:windows "DevIL.dll")))


(defun load-dlls ()
  (handler-bind ((style-warning #'muffle-warning))
    (cffi:load-foreign-library 'sdl)
    (cffi:load-foreign-library 'freetype)
    (cffi:load-foreign-library 'glft)
    (cffi:load-foreign-library 'opengl)
    (cffi:load-foreign-library 'devil-il)))

(defun unload-dlls ()
  (handler-bind ((style-warning #'muffle-warning))
    (cffi:close-foreign-library 'glft)
    (cffi:close-foreign-library 'devil-il)
    (cffi:close-foreign-library 'opengl)
    (cffi:close-foreign-library 'freetype)
    (cffi:close-foreign-library 'sdl)))
